/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.indexer.impl;

import java.util.logging.Logger;

import org.eclipse.jetty.util.log.Log;

import edu.ucsd.db.bassarisk.indexer.api.IPosting.IPositionedPosting;
import edu.ucsd.db.bassarisk.indexer.api.IPosting.ISequencedPosting;
import edu.ucsd.db.bassarisk.indexer.api.IPostingFactory;
import edu.ucsd.db.bassarisk.indexer.api.IPostingHandler;

public class Positioner implements IPostingHandler<ISequencedPosting> {
  public final Logger LOG = Logger.getLogger(this.getClass().getName());

  private final IPostingFactory postingFactory;
  private final IPostingHandler<IPositionedPosting> postingSink;
  private int position;

  public Positioner(final IPostingHandler<IPositionedPosting> postingSink,
                    final IPostingFactory postingFactory) {
    this.postingFactory = postingFactory;
    this.postingSink = postingSink;
    this.position = 0;
  }

  @Override
  public void post(final ISequencedPosting posting) {
//    LOG.info("Processing posting=" + posting.toString() + " p=" + this.position);
    this.postingSink.post(this.postingFactory.posting(posting.getGroupKey(),
                                                      posting.getTokenKey(),
                                                      posting.getValue(),
                                                      this.position));
    if (posting.consumesSpace()) {
      this.position++;
    }
  }

}
